<div class="row">
	<div class="col-md-12">
		<ol class="breadcrumb">
			<li>
				<a href="#" onclick="toHome()">
					主页
				</a>
			</li>
			<li class="active">
				通兑卡列表
			</li>
		</ol>
	</div>
</div>
<form role="form" class="form-horizontal">
	<div class="row page-cont">
		<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cont-top">
			<div class="panel panel-white">
				<div class="panel-heading">
					<h4 class="panel-title">
						<span class="title-text">检索通兑卡</span>
					</h4>
					<div class="panel-tools">
						<div class="dropdown">
							<a data-toggle="dropdown" class="btn btn-xs dropdown-toggle btn-transparent-grey">
								<i class="fa fa-cog"></i>
							</a>
							<ul class="dropdown-menu dropdown-light pull-right" role="menu">
								<li>
									<a class="panel-collapse collapses" href="#"><i class="fa fa-angle-up"></i> <span>折叠</span> </a>
								</li>
								<li>
									<a class="panel-expand" href="#">
										<i class="fa fa-expand"></i> <span>全屏</span>
									</a>
								</li>
							</ul>
						</div>
						<a class="btn btn-xs btn-link panel-close" href="#">
							<i class="fa fa-times"></i>
						</a>
					</div>
				</div>
				<div class="panel-body"> 
					<div class="form-group panel-cont">
					    <label class="col-sm-2 control-label">通兑卡名称</label>
					    <div class="col-sm-4">
					      <input type="text" class="form-control" id="couponName" />
					    </div>
				  	</div>
					<div class="form-group panel-cont">
					    <label for="coupon" class="col-sm-2 control-label">通兑卡应用场景</label>
					    <div class="col-sm-4">
					      	<select class="">
							  	<option value="全部">全部</option>
							  	<option value="通兑卡">通兑卡</option>
							  	<option value="红包">红包</option>
							</select>
					    </div>
				  	</div>
					<div class="form-group panel-cont">
					    <label for="startDate" class="col-sm-2 control-label">开始时间</label>
					    <div class="col-sm-2 selectDate">
					   		<input type='text' readonly="readonly" class='form-control selectData' id='startDate' name='date' value='' />
					   		<i class="fa fa-calendar"></i>  
					    </div>
					    <label for="endDate" class="col-sm-2 control-label">结束时间</label>
					    <div class="col-sm-2 selectDate">
					    	<input type='text' readonly="readonly" class='form-control selectData' id='endDate' name='date' value='' />
					    	<i class="fa fa-calendar"></i>
					    </div>
					    <span class="col-sm-4 tips">
					    	<i class="fa fa-info-circle"></i>
					    	请选择创建日期的区间
					    </span>
				  	</div>
				  	<div class="form-group">
				  		<div class="col-sm-5 col-sm-offset-5">
				  			<button type="button" class="btn btn-primary">查询</button>
				  			<button type="reset" class="btn">重置</button>
				  		</div>
				  	</div>
				</div>
			</div>
		</div>
		<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cont-bottom">
			<div class="panel panel-white">
				<div class="panel-heading">
					<h4 class="panel-title">
						<span class="title-text">通兑卡列表</span>
					</h4>
					<div class="panel-tools">
						<div class="dropdown">
							<a data-toggle="dropdown" class="btn btn-xs dropdown-toggle btn-transparent-grey">
								<i class="fa fa-cog"></i>
							</a>
							<ul class="dropdown-menu dropdown-light pull-right" role="menu">
								<li>
									<a class="panel-collapse collapses" href="#"><i class="fa fa-angle-up"></i> <span>折叠</span> </a>
								</li>
								<li>
									<a class="panel-expand" href="#">
										<i class="fa fa-expand"></i> <span>全屏</span>
									</a>
								</li>
							</ul>
						</div>
						<a class="btn btn-xs btn-link panel-close" href="#">
							<i class="fa fa-times"></i>
						</a>
					</div>
				</div>
				<div class="row table-btn">
					<div class="col-sm-12">
						<button type="button" class="btn btn-success add-btn">新增&nbsp;<i class="fa fa-plus"></i></button>
					</div>
				</div>
				<div class="panel-body table-body">											
					<table class="table table-bordered table-hover">
						<thead>
							<tr>
								<th class="hidden-xs">id</th>
								<th class="hidden-xs">通兑卡卡号</th>
								<th class="hidden-xs">状态</th>
								<th class="hidden-xs">兑换时间</th>
								<th class="hidden-xs">创建时间</th>
								<th class="hidden-xs">更新时间</th>
								<th class="hidden-xs">操作</th>
							</tr>
						</thead>
						<tbody>
							
						</tbody>
					</table>
					<div class="M-box3 page-style"></div>
				</div>
			</div>
		</div>
	</div>
</form>

<script>
	/***************************************变量***************************************/
	var pageNo = 1;    // 第几页
	var pageSize = 10; // 每页几条
	var totalPage;     // 总页数
	var totalData;     // 总条数	
	var token = MXCookie.get("token",token);  // 登陆token;
	var username = GetQueryString("username"); // 用户名		
	/***************************************事件***************************************/
	datePicker($("#startDate"));  // 开始时间
	datePicker($("#endDate"));    // 结束时间

	// 添加优惠券
	toOperat($(".add-btn"),"view/exchangeCard/add-card.html");	


	// 不删除
	$("body").on("click",".ask-no",function(){
		$(".ask-alert").fadeOut();	        	
    });		

    // 关闭弹框
	$("body").on("click",".close-icon",function(){
		$(".ask-alert").fadeOut();	        	
    });			

	/***************************************函数***************************************/
	// 确定删除
	function dialogyYesBtn () {
		var cardId = $(".ask-alert").attr("data-id");
		var index = $(".ask-alert").attr("data-index");
    	deleteList(cardId,index);	
	}	
	// 获取通兑卡列表
	function getList() {
		$("tbody").html("");
		$.ajax({
			url: localStorage.grobalUrl + "/discount/exchangeCardList",
			type: "post",
			async:false,
			data:{
				username: username,
				token: token, 
				pageNo: pageNo,   
				pageSize: pageSize
			},
			success: function(data){
				console.log(data);
				var str = '<tr align="center">'+
								'<td class="excard-id"></td>'+
								'<td class="excard-number"></td>'+
								'<td class="excard-status"></td>'+
								'<td class="exchange-time"></td>'+
								'<td class="create-time"></td>'+
								'<td class="update-time"></td>'+
								'<td>'+
									'<button type="button" class="btn btn-dark-yellow check-btn">查看&nbsp;<i class="fa fa-eye"></i></button>'+
									'<button type="button" class="btn btn-dark-yellow cehck-pwd">查看密码&nbsp;<i class="fa fa-eye"></i></button>'+
									'<button type="button" class="btn btn-red delete-btn">删除&nbsp;<i class="fa fa-trash-o"></i></button>'+
								'</td>'+
							'</tr>'
				if(data.errCode == "00005"){
					tips("请重新登陆");
					setTimeout(function(){
						window.location.href = "login_login.html";
					},500);
				}else if (data.exchangeCardList){
				  	var dataVal = data.exchangeCardList.list;
					totalPage = data.exchangeCardList.totalPage;
			        totalData = data.exchangeCardList.totalRow;				  	
				  	if(dataVal.length == "0"){
				  		$("tbody").append("<tr><td align='center' colspan='7'>暂无数据</td></tr>");
				  	}
			        for(var i = 0; i < dataVal.length; i ++){
			            var cardVal = dataVal[i];
			            $("tbody").append(str);
			            $(".excard-id").eq(i).text(cardVal.id);
			            $(".excard-number").eq(i).text(cardVal.card_no);
			            if(cardVal.status == "DELETED"){
			            	$(".excard-status").eq(i).text("已删除");
			            	$(".delete-btn").eq(i).hide();
			            }else if(cardVal.status == "NOT_USE"){
			            	$(".excard-status").eq(i).text("未兑换");
			            }else{
			            	$(".excard-status").eq(i).text("已兑换");
			            	$(".delete-btn").eq(i).hide();
			            }
			            if(cardVal.use_time != "null"){
			            	$(".exchange-time").eq(i).text(cardVal.use_time);
			            }else{
			            	$(".exchange-time").eq(i).text("");
			            }
			            $(".create-time").eq(i).text(cardVal.create_time);
			            $(".update-time").eq(i).text(cardVal.update_time);
			        }
			  
			        // 删除按钮
			        $(".delete-btn").click(function(){
			            var index = $(".delete-btn").index(this);
			            var cardId = $(".excard-id").eq(index).text();
			            askAlert("是否要删除");
			            $(".ask-alert").attr("data-id",cardId).attr("data-index",index);
			        });

			        // 查看
			        jumpTo($(".check-btn"),"view/exchangeCard/card-view.html");
			  
			        // 查看密码
			        jumpTo($(".cehck-pwd"),"view/exchangeCard/card-pwd-view.html");
			  
				}else{
				  $("tbody").append("<tr><td align='center' colspan='7'>暂无数据</td></tr>");
				}
					
			},
			error: function(error){
				console.log(error);
			}
		});	
	}
	getList();

	// 分页
	pageSetGet();

	// 跳转（查看/生成优惠券）
	function jumpTo(tag,pageUrl){
		tag.click(function(){
	        var index = tag.index(this);
	        cardId = $(".excard-id").eq(index).text();
	        $(".pageAdd").attr("data-id",cardId);
	        url = pageUrl;
	        getPage(url); 
	    });		
	}		

	// 删除
	function deleteList(cardId,index){
		$.ajax({
			url: localStorage.grobalUrl + "/discount/deleteExchangeCard",
			type:"post",
			data: {
				username: username,
				token: token,				
				id: cardId
			},
			success: function(data){
				console.log(data);
				if(data.errMsg == "删除成功"){
					$(".delete-btn").eq(index).hide();
					$(".excard-status").eq(index).text("已删除");
					$(".ask-alert").fadeOut();
					tips("已删除");
				}else if(data.errCode == "00005"){
					tips("请重新登陆");
					setTimeout(function(){
						window.location.href = "login_login.html";
					},500);					
				}else{
					tips("删除失败");
					$(".ask-alert").fadeOut();
				}
			},
			error: function(error){
				console.log(error);
			}
		});		
	}

	


	
	
</script>
